clear
set more off
global dirin ""
cd "$dirin"
global dirout ""

use Puebla_weekly_introcash_analysis.dta, clear
gen ban=(all_dates>=21157)
tsset id all_dates
bysort id: gen temp1=all_dates if _n==1
bysort id: egen cohort=max(temp1)
gen tenure=((all_dates-cohort)) +1
gcollapse (max) max_tenure=tenure max_all_dates=all_dates (min) min_tenure=tenure min_all_dates=all_dates  (mean) cohort  (sum) cash_trips epaid_trips cash_fares_usd epaid_fares_usd, by(id ban) fast 
egen total_fares=rowtotal(cash_fares_usd epaid_fares_usd)
egen total_trips=rowtotal(cash_trips epaid_trips)
gen temp=cash_trips/total_trips if ban==0
bysort id: egen share_cash=max(temp)
drop temp
gen t1=(max_all_dates-min_all_dates)/7 +1
order id ban max_tenure min_tenure t1
gen mean_trips=(total_trips/t1)
gen mean_trip_credit=(epaid_trips/t1)
gen mean_total_fares=(total_fares/t1)
save temp2.dta, replace

use temp2.dta, clear
tsset id ban
capture drop temp
gen temp =total_fares if ban==0
bysort id: egen total_fares_bf_ban=max(temp)
drop temp
bysort id: egen min_ban=min(ban)
keep if min_ban==0
drop min_ban
capture drop temp
gen temp=epaid_trips if ban==0
bysort id: egen min_epaid_trips=min(epaid_trips)
keep if min_epaid_trips!=0
drop min_epaid_trips
tsfill, full
replace mean_trips=0 if mean_trips==.
replace mean_trip_credit=0 if mean_trip_credit==.
replace share_cash=l.share_cash if share_cash==.
replace total_fares_bf_ban=l.total_fares_bf_ban if total_fares_bf_ban==.
replace cohort=l.cohort if cohort==.
format cohort %td

gen ln_mean_trips=ln(mean_trips)
gen ln_mean_trip_credit=ln(mean_trip_credit)
gen ln_total_fares_bf_ban=ln(total_fares_bf_ban)
gen share_cash_ban=share_cash*ban
egen time=group(ban)
tsset id time
gen dh_mean_trips=2*(mean_trips-l.mean_trips)/(mean_trips+l.mean_trips)
gen dh_mean_trip_credit=2*(mean_trip_credit-l.mean_trip_credit)/(mean_trip_credit+l.mean_trip_credit)
gen diff_tenure=(max_tenure-min_tenure)
bysort id: egen min_diff_tenure=min(diff_tenure)
bysort id: egen min_total_trips=min(total_trips)

capture drop flag_*
gen flag_1=0
replace flag_1=1 if min_total_trips<11

capture drop flag_2
gen flag_2=0
replace flag_2=1 if min_total_trips<5

capture drop flag_3
gen flag_3=0
replace flag_3=1 if min_total_trips<3
capture drop share_cash2
gen share_cash2=share_cash*share_cash
label variable share_cash "Share cash (t-1)"
label variable share_cash2 "Share cash squared (t-1)"
label variable ln_total_fares_bf_ban "Log fares (t-1)"

drop if share_cash==0

reghdfe dh_mean_trips share_cash ln_total_fares_bf_ban, absorb(i.cohort) vce(robust)
	outreg2 using "$dirout/TableD2.tex", keep(share_cash ln_total_fares_bf_ban) ///
	tex nocons replace bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, No) label nonotes

reghdfe dh_mean_trips share_cash ln_total_fares_bf_ban if flag_3==0, absorb(i.cohort) vce(robust)
	outreg2 using "$dirout/TableD2.tex", keep(share_cash ln_total_fares_bf_ban) ///
	tex nocons append bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, At least 3) label nonotes
	
reghdfe dh_mean_trips share_cash ln_total_fares_bf_ban if flag_2==0, absorb(i.cohort) vce(robust)
	outreg2 using "$dirout/TableD2.tex", keep(share_cash ln_total_fares_bf_ban) ///
	tex nocons append bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, At least 5) label nonotes
	
reghdfe dh_mean_trips share_cash ln_total_fares_bf_ban if  flag_1==0, absorb(i.cohort) vce(robust)
	outreg2 using "$dirout/TableD2.tex", keep(share_cash ln_total_fares_bf_ban) ///
	tex nocons append bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, At least 10) label nonotes	

	

reghdfe dh_mean_trip_credit share_cash ln_total_fares_bf_ban , absorb(i.cohort)  vce(robust)
	outreg2 using "$dirout/TableD3.tex", keep(share_cash ln_total_fares_bf_ban) ///
	tex nocons replace bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, No) label nonotes

reghdfe dh_mean_trip_credit share_cash ln_total_fares_bf_ban if flag_3==0, absorb(i.cohort)  vce(robust)
	outreg2 using "$dirout/TableD3.tex", keep(share_cash ln_total_fares_bf_ban) ///
	tex nocons append bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, At least 3) label nonotes
	
reghdfe dh_mean_trip_credit share_cash ln_total_fares_bf_ban if flag_2==0, absorb(i.cohort)  vce(robust)
	outreg2 using "$dirout/TableD3.tex", keep(share_cash ln_total_fares_bf_ban) ///
	tex nocons append bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, At least 5) label nonotes
	
reghdfe dh_mean_trip_credit share_cash ln_total_fares_bf_ban if flag_1==0, absorb(i.cohort)  vce(robust)
	outreg2 using "$dirout/TableD3.tex", keep(share_cash ln_total_fares_bf_ban) ///
	tex nocons append bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, At least 10) label nonotes	
	
***********************************************************************************************************************
***********************************************************************************************************************
***********************************************************************************************************************
***********************************************************************************************************************

use Puebla_weekly_introcash_analysis.dta, clear
drop id new_user temp_all_dates
drop if all_dates>21157  
rename owner_uuid client_uuid
replace epaid_fares_usd=. if epaid_fares_usd==0
replace cash_fares_usd=. if cash_fares_usd==0
drop if epaid_fares_usd==. & cash_fares_usd==.
drop if epaid_fares_usd==0 & cash_fares_usd==0
drop if cash_trips==0 & epaid_trips==0
drop if cash_trips==. & epaid_trips==.

gen CARD=1 if epaid_fares_usd>0 & epaid_fares_usd!=.
gen CASH=1 if cash_fares_usd>0 & cash_fares_usd!=.

sort client all_dates
by client: gen n=_n 
by client: gen n_CARD=_n if CARD!=.
by client: gen n_CASH=_n if CASH!=.
by client: egen min_CARD=min(n_CARD)
by client: egen min_CASH=min(n_CASH)


gen type_CARD=1 if n==min_CARD
gen type_CASH=1 if n==min_CASH
replace type_CARD=0 if type_CARD==.
replace type_CASH=0 if type_CASH==.
gen type=type_CARD + type_CASH
bysort client_uuid: gen sum_type=sum(type)
tab sum_type

by client: egen sum_cash_fares_usd=sum(cash_fares_usd)
by client: egen sum_epaid_fares_usd=sum(epaid_fares_usd)
by client: egen sum_cash_trips=sum(cash_trips)
by client: egen sum_epaid_trips=sum(epaid_trips)
bysort client: egen min_all_dates=min(all_dates)
gen tenure = (21157-min_all_dates)
replace tenure=tenure/7
replace tenure=tenure + 1
gcollapse (sum) cash_trips epaid_trips cash_fares_usd epaid_fares_usd (mean) sum_cash_fares_usd sum_epaid_fares_usd sum_cash_trips sum_epaid_trips tenure, by(client_uuid) fast

egen total_fares=rowtotal(cash_fares_usd epaid_fares_usd)
egen total_trips=rowtotal(cash_trips epaid_trips)
gen share_cash_fares=cash_fares_usd/total_fares
gen share_cash_trips=cash_trips/total_trips
keep if total_trips>5
capture drop cash_share_fares_bins
gen cash_share_fares_bins=autocode(share_cash_fares,11,-.1,1)
su cash_share_fares_bins, d
save temp.dta, replace


use Puebla_weekly_introcash_analysis.dta, clear
drop id new_user temp_all_dates
rename owner_uuid client_uuid
drop if all_dates<=21157  
keep client_uuid 
duplicates drop
merge 1:1 client_uuid using temp.dta
drop if _merge==1
gen survivied=(_merge==3)
egen hist_fares=rowtotal(sum_cash_fares_usd sum_epaid_fares_usd)
egen hist_trips=rowtotal(sum_cash_trips sum_epaid_trips)
gen lntenure=ln(tenure)
gen lnhist_fares=ln(hist_fares/tenure)
gen lnhist_trips=ln(hist_trips/tenure)

drop if share_cash_fares==0

label variable share_cash_fares "Share cash (t-1)"
label variable lnhist_fares "Log fares (t-1)"

reghdfe survivied share_cash_fares  lnhist_fares, absorb(i.tenure)  vce(robust)
	outreg2 using "$dirout/TableD4.tex", keep(share_cash_fares lnhist_fares) ///
	tex nocons append bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, No) label nonotes

reghdfe survivied share_cash_fares lnhist_fares if hist_trips>2, absorb(i.tenure)  vce(robust)
	outreg2 using "$dirout/TableD4.tex", keep(share_cash_fares lnhist_fares) ///
	tex nocons append bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, At least 3) label nonotes
	
reghdfe survivied share_cash_fares lnhist_fares if hist_trips>4, absorb(i.tenure)  vce(robust)
	outreg2 using "$dirout/TableD4.tex", keep(share_cash_fares lnhist_fares) ///
	tex nocons append bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, At least 5) label nonotes
	
reghdfe survivied share_cash_fares lnhist_fares if hist_trips>9, absorb(i.tenure)  vce(robust)
	outreg2 using "$dirout/TableD4.tex", keep(share_cash_fares lnhist_fares) ///
	tex nocons append bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, At least 10) label nonotes	

	
	
***********************************************************************************************************************
***********************************************************************************************************************
***********************************************************************************************************************
***********************************************************************************************************************


use temp2.dta, clear
tsset id ban
capture drop temp
gen temp =total_fares if ban==0
bysort id: egen total_fares_bf_ban=max(temp)
drop temp
format cohort %td
gen ln_mean_trips=ln(mean_trips)
gen ln_mean_trip_credit=ln(mean_trip_credit)
gen ln_total_fares_bf_ban=ln(total_fares_bf_ban)
gen share_cash_ban=share_cash*ban
egen time=group(ban)
tsset id ban
gen d_ln_mean_trips=ln_mean_trips-l.ln_mean_trips
gen p_mean_trips=(mean_trips-l.mean_trips)/l.mean_trips
gen dh_mean_trips=2*(mean_trips-l.mean_trips)/(mean_trips+l.mean_trips)
gen dh_mean_trip_credit=2*(mean_trip_credit-l.mean_trip_credit)/(mean_trip_credit+l.mean_trip_credit)
gen diff_tenure=(max_tenure-min_tenure)
bysort id: egen min_diff_tenure=min(diff_tenure)
bysort id: egen min_total_trips=min(total_trips)
capture drop flag_*
gen flag_1=0
replace flag_1=1 if min_total_trips<11
capture drop flag_2
gen flag_2=0
replace flag_2=1 if min_total_trips<5
capture drop flag_3
gen flag_3=0
replace flag_3=1 if min_total_trips<3
capture drop share_cash2
gen share_cash2=share_cash*share_cash
label variable share_cash "Share cash (t-1)"
label variable share_cash2 "Share cash squared (t-1)"
label variable ln_total_fares_bf_ban "Log fares (t-1)"
drop if share_cash==0



reghdfe dh_mean_trips share_cash ln_total_fares_bf_ban, absorb(i.cohort) vce(robust)
	outreg2 using "$dirout/TableD5.tex", keep(share_cash ln_total_fares_bf_ban) ///
	tex nocons replace bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, No) label nonotes

reghdfe dh_mean_trips share_cash ln_total_fares_bf_ban if flag_3==0, absorb(i.cohort) vce(robust)
	outreg2 using "$dirout/TableD5.tex", keep(share_cash ln_total_fares_bf_ban) ///
	tex nocons append bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, At least 3) label nonotes
	
reghdfe dh_mean_trips share_cash ln_total_fares_bf_ban if flag_2==0, absorb(i.cohort) vce(robust)
	outreg2 using "$dirout/TableD5.tex", keep(share_cash ln_total_fares_bf_ban) ///
	tex nocons append bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, At least 5) label nonotes
	
reghdfe dh_mean_trips share_cash ln_total_fares_bf_ban if  flag_1==0, absorb(i.cohort) vce(robust)
	outreg2 using "$dirout/TableD5.tex", keep(share_cash ln_total_fares_bf_ban) ///
	tex nocons append bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, At least 10) label nonotes	



reghdfe dh_mean_trip_credit share_cash ln_total_fares_bf_ban , absorb(i.cohort)  vce(robust)
	outreg2 using "$dirout/TableD6.tex", keep(share_cash ln_total_fares_bf_ban) ///
	tex nocons replace bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, No) label nonotes

reghdfe dh_mean_trip_credit share_cash ln_total_fares_bf_ban if flag_3==0, absorb(i.cohort)  vce(robust)
	outreg2 using "$dirout/TableD6.tex", keep(share_cash ln_total_fares_bf_ban) ///
	tex nocons append bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, At least 3) label nonotes
	
reghdfe dh_mean_trip_credit share_cash ln_total_fares_bf_ban if flag_2==0, absorb(i.cohort)  vce(robust)
	outreg2 using "$dirout/TableD6.tex", keep(share_cash ln_total_fares_bf_ban) ///
	tex nocons append bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, At least 5) label nonotes
	
reghdfe dh_mean_trip_credit share_cash ln_total_fares_bf_ban if flag_1==0, absorb(i.cohort)  vce(robust)
	outreg2 using "$dirout/TableD6.tex", keep(share_cash ln_total_fares_bf_ban) ///
	tex nocons append bdec(3) tdec(3)  ctitle(" ") ///
	addtext(Cohort, Yes, Min. Trips, At least 10) label nonotes	


	
